
package com.huaweicloud.haydn.delivercore.agent.service;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @since 2022-10-18
 */
@Component
@Slf4j
public class CommonUtils {

    public String sqlQueryBuild(String rawQuery) {
        if (StringUtils.isBlank(rawQuery)) {
            return "";
        }
        String[] split = rawQuery.split(",");

        StringBuilder sb = new StringBuilder();

        if (split.length == 1) {
            sb.append("(");
            sb.append("'");
            sb.append(split[0]);
            sb.append("'");
            sb.append(")");
            return sb.toString();
        }

        for (int i = 0; i < split.length; i++) {
            String subStr = split[i];
            if (i == 0) {
                sb.append("(");
                sb.append("'");
                sb.append(subStr);
                sb.append("'");
                sb.append(",");
            } else if (i == split.length - 1) {
                sb.append("'");
                sb.append(subStr);
                sb.append("'");
                sb.append(")");
            } else {
                sb.append("'");
                sb.append(subStr);
                sb.append("'");
                sb.append(",");
            }
        }
        return sb.toString();
    }
}
